//
//  LeanCloudObject.m
//  OneDay
//
//  Created by lanou3g on 15/11/10.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "LeanCloudObject.h"

@implementation LeanCloudObject

// 创建并插入
- (void)creatObjectWithTitle:(NSString *)title imageUrl:(NSString *)imageUrl needString:(id)needStr style:(NSString *)style  userName:(NSString *)userName
{
    if ([style isEqualToString:@"party"]) {
        PartyObject *party = [[PartyObject alloc] init];
        party.title = title;
        party.imageUrl = imageUrl;
        party.needStr = needStr;
        party.userName = userName;
        // 一般用子线程上传
        [party saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"创建并上传成功");
            }
        }];
    }else if ([style isEqualToString:@"video"]) {
        VideoObject *video = [[VideoObject alloc] init];
        video.title = title;
        video.imageUrl = imageUrl;
        video.needStr = needStr;
        video.userName = userName;
        [video saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"创建并上传成功");
            }
        }];
    }else if ([style isEqualToString:@"web"]) {
        WebObject *web = [[WebObject alloc] init];
        web.title = title;
        web.imageUrl = imageUrl;
        web.needStr = needStr;
        web.userName = userName;
        [web saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"创建并上传成功");
            }
        }];
    }else {
        ArticleObject *article = [[ArticleObject alloc] init];
        article.title = title;
        article.imageUrl = imageUrl;
        article.needStr = needStr;
        article.userName = userName;
        [article saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"创建并上传成功");
            }
        }];
    }
}

// 查询
- (NSArray *)selectStundetWithStyle:(NSString *)style userName:(NSString *)userName
{
    if ([style isEqualToString:@"party"]) {
        AVQuery *query = [PartyObject query];
        NSArray *partyArray = [query findObjects];
        NSMutableArray *needArray = [NSMutableArray array];
        for (ArticleObject *article in partyArray) {
            if ([article.userName isEqualToString:userName]) {
                [needArray addObject:article];
            }
        }
        return needArray;
    }else if ([style isEqualToString:@"video"]) {
        AVQuery *query = [VideoObject query];
        NSArray *videoArray = [query findObjects];
        NSMutableArray *needArray = [NSMutableArray array];
        for (ArticleObject *article in videoArray) {
            if ([article.userName isEqualToString:userName]) {
                [needArray addObject:article];
            }
        }
        return needArray;
    }else if ([style isEqualToString:@"web"]) {
        AVQuery *query = [WebObject query];
        NSArray *webArray = [query findObjects];
        NSMutableArray *needArray = [NSMutableArray array];
        for (ArticleObject *article in webArray) {
            if ([article.userName isEqualToString:userName]) {
                [needArray addObject:article];
            }
        }
        return needArray;
    }else {
        AVQuery *query = [ArticleObject query];
        NSArray *articleArray = [query findObjects];
        NSMutableArray *needArray = [NSMutableArray array];
        for (ArticleObject *article in articleArray) {
            if ([article.userName isEqualToString:userName]) {
                [needArray addObject:article];
            }
        }
        return needArray;
    }
    
//    // 方法1
//    [query findObjects];
//    // 方法2
//    NSArray *array =  [query findObjects:nil];
}

// 删除
- (void)deleteStudentObjectWithStyle:(NSString *)style userName:(NSString *)userName title:(NSString *)title
{
    NSMutableArray *userArray = (NSMutableArray *)[self selectStundetWithStyle:style userName:userName];
    if ([style isEqualToString:@"party"]) {
        NSString *ID;
        for (PartyObject *party in userArray) {
            if ([party.title isEqualToString:title]) {
                ID = party.objectId;
                break;
            }
        }
        PartyObject *partyObject = [PartyObject objectWithoutDataWithObjectId:ID];
        [partyObject deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"删除成功");
            }
        }];
    }else if ([style isEqualToString:@"video"]) {
        NSString *ID;
        for (VideoObject *video in userArray) {
            if ([video.title isEqualToString:title]) {
                ID = video.objectId;
                break;
            }
        }
        VideoObject *videoObject = [VideoObject objectWithoutDataWithObjectId:ID];
        [videoObject deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"删除成功");
            }
        }];
    }else if ([style isEqualToString:@"web"]) {
        NSString *ID;
        for (WebObject *web in userArray) {
            if ([web.title isEqualToString:title]) {
                ID = web.objectId;
                break;
            }
        }
        WebObject *webObject = [WebObject objectWithoutDataWithObjectId:ID];
        [webObject deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"删除成功");
            }
        }];
    }else {
        NSString *ID;
        for (ArticleObject *article in userArray) {
            if ([article.title isEqualToString:title]) {
                ID = article.objectId;
                break;
            }
        }
        ArticleObject *articleObject = [ArticleObject objectWithoutDataWithObjectId:ID];
        [articleObject deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"删除成功");
            }
        }];
    }
}




@end
